<template>
  <div>
    <el-descriptions title="个人信息基本资料" border>
      <!-- 
      ：rowspan：合并单元格
      ：width：设置宽度
      ：label：设置标题
      ：align：设置对齐方式
      ：direction：设置方向
      ：before-close：关闭前执行
       -->
      <el-descriptions-item
        :rowspan="2"
        :width="140"
        label="个人头像"
        align="center"
      >
        <el-image style="width: 100px; height: 100px" :src="user.user_pic" />
      </el-descriptions-item>
      <el-descriptions-item label="用户名">{{
        user.username
      }}</el-descriptions-item>
      <el-descriptions-item label="用户邮箱">{{
        user.email
      }}</el-descriptions-item>
      <el-descriptions-item label="昵称">{{
        user.nickname
      }}</el-descriptions-item>
      <el-descriptions-item label="创建时间"
        ><el-tag size="small">{{ user.create_time }}</el-tag>
      </el-descriptions-item>
      <el-descriptions-item label="操作">
        <el-button
          type="primary"
          style="margin-left: 16px"
          @click="xiugaixinxi"
        >
          修改信息
        </el-button>
      </el-descriptions-item>
    </el-descriptions>
    <el-drawer
      v-model="drawer"
      title="修改信息"
      :direction="direction"
      :before-close="handleClose"
    >
    <!--抽屉-->
      <el-form
        :model="form"
        label-width="auto"
        style="max-width: 400px"
        ref="formRef"
        :rules="xx"
        size="large"
      >
        <el-form-item label="用户名" >
          <el-input v-model="form.username" disabled/>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" />
        </el-form-item>

        <el-form-item label="昵称" prop="nickname">
          <el-input v-model="form.nickname" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="xiugai(formRef)"
            >确定修改</el-button
          >
        </el-form-item>
      </el-form>
    </el-drawer>
  </div>
</template>

<script lang="ts" setup>
import {
  Check,
  Delete,
  Edit,
  Message,
  Search,
  Star,
} from "@element-plus/icons-vue"; //按钮
import { ref, onMounted } from "vue";
import { ElMessageBox,ElMessage} from "element-plus";
import type { DrawerProps } from "element-plus";
import { zlcha ,zlxiu } from "../api/login.js"; //导入js
import { cuser } from "../stores/user.js";


const ss = cuser(); //获取用户id
const drawer = ref(false); //抽屉
const form = ref({});
// 关闭按钮
const handleClose = (done: () => void) => {
  ElMessageBox.confirm("您的修改还未保存,确定退出?", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
  }).then(() => {
    done();
    form.value = {};
    into();
  });
};
// 邮箱校验
const xx = ref({
  email: [
    { required: true, message: "请输入邮箱", trigger: "blur" },
    {
      pattern: /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/,
      message: "请输入正确的邮箱格式",
      trigger: "blur",
    },
  ],
  nickname: [{ required: true, message: "请输入昵称", trigger: "blur" },
    { min: 2, max: 10, message: "长度在 2 到 10 个字符", trigger: "blur" },
  ]
});

//查用户数据
const user = ref({});
//查用户数据方法
const into = async () => {
  const result = await zlcha(ss.user.id);
  console.log(result);
  user.value = result.data; //赋值
  console.log(user.value);
};
//确认修改方法  弹框
const xiugaixinxi = () => {
  drawer.value = true;
form.value.email = user.value.email;
form.value.nickname = user.value.nickname;
form.value.username = user.value.username;
form.value.id= user.value.id;
};

const tijao = async() => {
  const result = await zlxiu(form.value);
  console.log(result);
  if (result.code == 20041) {
    ss.user.nickname = form.value.nickname;
    ElMessage({
      type: "success",
      message: "修改成功",
    });
    drawer.value = false;
    into();
  }else{
    ElMessage({
      type: "error",
      message: "修改失败，请稍后再试",
    });
  }

};
const formRef = ref(null);
//确认修改按钮  提交按钮
const xiugai = (formRef) => {
  formRef.validate((valid) => {
    if (valid) {
      // 确认修改
      console.log("校验通过");
      tijao();
    }else{
      console.log("校验失败");
    }

  });
};
import { useRouter } from 'vue-router'
const router = useRouter();
import {cs} from "../stores/zhuangtai.js";
const zt=cs()
onMounted(()=>{
  if(zt.zhuangtai==""){
    ElMessage({
      message: "请先登录",
      type: "warning",
    });
    router.push("/")
  }
})



// 页面加载时执行获取个人资料表
onMounted(() => {
  if(zt.zhuangtai==""){
    ElMessage({
      message: "请先登录",
      type: "warning",
    });
    router.push("/")
  }
  into();
});
</script>

<style></style>
